package com.qishenyuan.competition.model;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.qishenyuan.competition.dto.CreateTopicReq;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qishenyuan
 * @since 2025-03-02
 */
@TableName("teamup")
@Data
public class Teamup implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键 ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 用户唯一标识
     */
    private String uid;

    /**
     * 帖子内容
     */
    private String content;

    /**
     * 创建时间（时间戳）
     */
    private Long createTime;

    /**
     * 修改时间（时间戳）
     */
    private Long modifyTime;

    /**
     * 图片地址，多个图片用逗号分隔
     */
    private String pictures;

    /**
     * 帖子标题
     */
    private String title;

    /**
     * 联系方式
     */
    private String contract;

    /**
     * 版本号，默认为 0
     */
    private Integer version;

    /**
     * 0：活动；1:竞赛
     */
    private Integer tag;

    /**
     * 0:找人；1:找项目
     */
    private Integer type;

    /**
     * 0：未删除；1:已删除
     */
    private Boolean isDelete;

    /**
     * 发帖人学校
     */
    private String school;

    public static Teamup buildModel(CreateTopicReq createTopicReq, User user) {
        Teamup teamup = new Teamup();
        BeanUtils.copyProperties(createTopicReq, teamup);
        teamup.setPictures(list2String(createTopicReq.getImageUrls()));
        teamup.setCreateTime(System.currentTimeMillis());
        teamup.setModifyTime(System.currentTimeMillis());
        teamup.setVersion(0);
        teamup.setIsDelete(false);
        teamup.setSchool(user.getSchool());
        return teamup;
    }

    // 把list转换成字符串
    private static String list2String(List<String> imageList) {
        StringBuilder sb = new StringBuilder();
        for (String image : imageList) {
            sb.append(image);
            sb.append(";");
        }
        return sb.toString();
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }

}
